package org.hackystat.iw.projectoverviewer.page.projects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.hackystat.iw.projectoverviewer.ProjectOverviewerApplication;
import org.hackystat.iw.projectoverviewer.jaxb.projects.Project;
import org.hackystat.iw.projectoverviewer.jaxb.projects.Projects;
import org.hackystat.iw.projectoverviewer.page.defaultmetrics.SetMetricsPage;
import org.hackystat.iw.projectoverviewer.page.newproject.NewProjectPage;

/**
 * A table showing all the current projects. It extends a ListView structure.
 * 
 * @author Jianfei Liao
 * @author Nathan Britton
 */
public class ProjectsTable extends ListView {
  /** Supports serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Constructor. List all projects that is inside the projects.xml file.
   * 
   * @param id Component ID, supplied by Wicket markup.
   */
  public ProjectsTable(String id) {
    super(id);
    
    // set the content list for this list view
    ProjectOverviewerApplication app = (ProjectOverviewerApplication)getApplication();
    Projects projects = app.getProjects();
    if (projects == null) {
      PageParameters param = new PageParameters();
      param.put("NoProject", true);
      this.setResponsePage(NewProjectPage.class, param);
    }
    else {
      // sort the projects according to the project names
      List<Project> projectsList = projects.getProject();
      List<String>  projectNamesList = new ArrayList<String>();
      
      for (Project project : projectsList) {
        projectNamesList.add(project.getProjectName());
      }
      
      Collections.sort(projectNamesList);
      
      List<Project> sortedProjectsList = new ArrayList<Project>();
      for (String name : projectNamesList) {
        for (Project project : projectsList) {
          if (name.equals(project.getProjectName())) {
            sortedProjectsList.add(project);
          }
        }
      }
      // set the sorted list as the list for the list view
      setList(sortedProjectsList);
    }
  }
  
  /**
   * Populates the item from the projects instance.
   * 
   * @param item The item representing the actual project.
   */
  @Override
  protected void populateItem(ListItem item) {
    PageParameters param = new PageParameters();
    Project project = (Project) item.getModelObject();
    param.put("projectName", project.getProjectName());
    
    // project name
    item.add(new Label("projectName", project.getProjectName()));
    // treemap link
    item.add(new BookmarkablePageLink("projectLink", SetMetricsPage.class, param));
    // delete link
    item.add(new DeleteLink("delete", project.getProjectName()));  
  }
}
